Ext.ux.ModifyCategory = Ext.extend(Ext.Window, {
	initComponent : function(config) {
		var basePath = document.getElementById('basePath').href;
		var linedata = Ext.getCmp("categoryDataGrid").getStore()
				.getAt(Ext.getCmp("categoryDataGrid").getSelectionModel().lastActive);
		var lang;
		var parent;
		if(null == linedata.json.parent) {
			parent = '';
		} else {
			parent = linedata.json.parent.id;
		}
		var exist;
		
		var langStore = new Ext.data.ArrayStore({
	        fields: ['lang', 'value'],
	        data : [['简体中文', 'zh_CN'],['繁体中文', 'zh_TW'],['美式英语', 'en_US']]
	    });
		
		var modifyForm = new Ext.FormPanel({
        	labelWidth : 100, // label settings here cascade unless overridden
			autoScroll:true,
			frame : true,
			region : 'center',
			border:true,
			fileUpload: true,
			bodyStyle : 'padding:5px 5px 0',
			width : '100%',
			defaults : {
				width : 220
			},
			defaultType : 'textfield',

			items : [{
				xtype : "combo",
				fieldLabel : '选择语言',
				store : langStore,
				id:'categoryLangUpdate',
				mode : 'local',
				valueField: 'value',
				displayField : 'lang',
				allowBlank : false,
				typeAhead : true,
				triggerAction : 'all',
				disabled :false,
				forceSelection : true,
				selectOnFocus : true,
				listeners:{
					select : function(combo, record, index) {
						lang = record.get('value');
					}
				}
			}, {
				xtype : "combo",
				fieldLabel : '选择父类别',
				store : new Ext.data.Store( {
					proxy: new Ext.data.HttpProxy({url: basePath + 'base/listParent.action?whoCategory='
							+ linedata.json.whoCategory + '&lang=' + linedata.json.lang}),
					reader:new Ext.data.ArrayReader({idIndex: 0},
						Ext.data.Record.create([{name: 'id'}, {name: 'name'}]))
				}),
				mode : 'local',
				valueField: 'value',
				displayField : 'name',
				typeAhead : true,
				id:'categoryParentUpdate',
				triggerAction : 'all',
				disabled :false,
				forceSelection : true,
				selectOnFocus : true,
				listeners:{
					select : function(combo, record, index) {
						Ext.getCmp('categoryParentUpdate').setValue(record.get('name'));
						parent = record.get('id');
					}
				}
			}, {
				fieldLabel : '类别名称',
				name : 'category.name',
				id:'categoryNameUpdate',
				allowBlank : false,
				listeners:{
					change :function(field) {
						Ext.Ajax.request( {
							url : encodeURI(encodeURI(basePath + 'base/checkExist.action?categoryName='
									+ field.getValue() + '&lang=' + linedata.json.lang)),
							method : 'post',
							callback : function(op, success, response) {
								var msg = eval('(' + response.responseText + ')');
								exist = msg.result;
								if (msg.result == 'true') {
									Ext.Msg.alert('提示', '该类别名已存在');
									return;
								}
							}
						});
					}
				}
			}, {
				fieldLabel : '类别归属',
				name : 'category.whoCategory',
				id:'categoryWhoUpdate',
				disabled :false,
				allowBlank : false
			}, {
				xtype:'textarea',
				fieldLabel : '类别简介',
				anchor:'95%',
				height:300,
				allowBlank : false,
				autoScroll:true,
				id:'categoryNotesUpdate',
				name : 'category.notes'
			}],
			bbar : ['->', {
				text : '保存',
				handler:function() {
					if(!modifyForm.form.isValid()) return;
					if(exist == 'true') {
						Ext.Msg.alert('提示', '该类别名已存在');
						return;
					}
					modifyForm.form.submit( {
						url : basePath + 'base/updateCategory.action?category.id=' + linedata.json.id + 
							'&category.lang=' + lang + '&parentId=' + parent,
						method : "POST",
						success : function(form, action) {
							form.reset();
							Ext.getCmp('modifyCategoryWin').close();
							Ext.MessageBox.alert('提示', '更新成功');
							Ext.StoreMgr.get('categoryStore').reload();
						},
						failure : function(form, action) {
							if (action.failureType == Ext.form.Action.SERVER_INVALID)
								Ext.MessageBox.alert('警告', action.result.msg);
						}
					});
				}
			}, {
				xtype:'spacer',
				width:50
			}]
		});
		
		Ext.Ajax.request( {
			url : basePath + 'base/loadCategory.action?category.id=' + linedata.json.id,
			method : 'post',
			callback : function(op, success, response) {
				var jsonObject = eval('(' + response.responseText + ')');
				lang = jsonObject.lang;
				Ext.getCmp("categoryNameUpdate").setValue(jsonObject.name);
    			Ext.getCmp("categoryWhoUpdate").setValue(jsonObject.whoCategory);
    			Ext.getCmp("categoryNotesUpdate").setValue(jsonObject.notes);
    			if(null != jsonObject.parent) {
    				Ext.getCmp("categoryParentUpdate").setValue(jsonObject.parent.name);
    			}
    			var langName;
    			switch(jsonObject.lang) {
    				case 'zh_CN' : langName = '简体中文'; break;
    				case 'zh_TW' : langName = '繁体中文'; break;
    				case 'en_US' : langName = '美式英文'; break;
    			}
    			Ext.getCmp("categoryLangUpdate").setValue(langName);
			}
		});
		
		Ext.apply(this, {
			title: "修改类别信息",
	        plain: true,
	        width:1000,
	        height:600,
	        modal: true,
	        maximizable:true,
	        border: false,
	        layout: 'border',
	        autoScroll:true,
	        items: [modifyForm]
		});
		Ext.ux.ModifyCategory.superclass.initComponent.apply(this, arguments);
	}
});
Ext.reg('lgnModifyCategory', Ext.ux.ModifyCategory);
